package com.renjun.smweb.service.InterfaceService.impl;

import com.alibaba.fastjson.JSONObject;
import com.renjun.smweb.commons.componet.BaseDaoComponent;
import com.renjun.smweb.commons.enums.EOperators;
import com.renjun.smweb.commons.global.StaticCaches;
import com.renjun.smweb.commons.util.BaseChecks;
import com.renjun.smweb.commons.util.CollectionUtils;
import com.renjun.smweb.commons.util.ResponseUtils;
import com.renjun.smweb.dao.bean.Parameter;
import com.renjun.smweb.dao.bean.QueryParams;
import com.renjun.smweb.dao.bean.UpdateParams;
import com.renjun.smweb.dao.pojo.LoginInfo;
import com.renjun.smweb.service.InterfaceService.InterfaceService;
import com.renjun.smweb.service.PermissionService.PermissionService;
import com.renjun.smweb.commons.componet.BaseDaoComponent;
import com.renjun.smweb.dao.bean.Parameter;
import com.renjun.smweb.service.InterfaceService.InterfaceService;
import com.renjun.smweb.service.PermissionService.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xlj on 2017/4/5.
 */
@Service
public class InterfaceServiceImpl implements InterfaceService {

    @Autowired
    private BaseDaoComponent baseDaoComponent;

    @Resource
    private PermissionService permissionService;

    @Override
    public JSONObject queryInterfaceList(Map<String, Object> params) throws SQLException {
        LoginInfo loginInfo = (LoginInfo) params.remove("userinfo");
        QueryParams queryInterFace = QueryParams.createQueryParams("V_M11_queryInterface_xlj");
        String name = (String) params.remove("name");
        if(!BaseChecks.hasEmptyStr(name)) {
            queryInterFace.addQueryParams(Parameter.createParameter("name", EOperators.类似,name));
        }
        String url = (String) params.remove("url");
        if(!BaseChecks.hasEmptyStr(url)) {
            queryInterFace.addQueryParams(Parameter.createParameter("url", EOperators.类似,url));
        }
        queryInterFace.addQueryParamsByMap(CollectionUtils.removeEmptyKeyByParams(params));
        try {
            queryInterFace.setDbLog(loginInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<Map<String, Object>> interFaceList = baseDaoComponent.selectDataByParams(queryInterFace);
        return ResponseUtils.createSuccessResponseBody("查询接口成功",interFaceList);
    }

    @Override
    public JSONObject modifyInterface(Map<String, Object> params) throws SQLException {
        LoginInfo loginInfo = (LoginInfo) params.remove("userinfo");
        Map<String,Object> interfaceMap = new HashMap<>();
        interfaceMap.put("status",params.remove("status"));
        UpdateParams updateInterFace = UpdateParams.createUpdateParams("t_funcurlinfo");
        updateInterFace.addParamsForMap(interfaceMap);
        updateInterFace.addWhereParameter(Parameter.createParameter("id",params.remove("id")));
        try {
            updateInterFace.setDbLog(loginInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        baseDaoComponent.updateDataByParams(updateInterFace);
        //刷新缓存
        List<Map<String, Object>> roleFunctions = permissionService.queryAllRoleFuncs();
        StaticCaches.loadAllFuncs(CollectionUtils.converMapKeyToLowerCase(roleFunctions));
        return ResponseUtils.createSuccessResponseBody("接口状态修改成功");
    }
}
